package com.fyzk.activity.lottery.zc.tz_dialog;

import android.content.Context;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.fyzk.activity.lottery.jc.football.JCInfo;
import com.tyjc.lottery.R;

import java.util.ArrayList;
import java.util.List;

public class ZCBF_Dialog_bqc extends ZC_Dialog_BaseActivity {


    //中间
    private View view;

    private LinearLayout ll_xxk;

    private GridView gr;
    private ZCBQC_DLG_GR_adapter adapter;
    private List<ZCBF_infor> list_bqc = new ArrayList<ZCBF_infor>();


    private String[] keyArray = null;

    public ZCBF_Dialog_bqc(Context context, JCInfo infos) {
        super(context, infos);
        init();
    }

    @Override
    protected void init() {
        addBiFen();
        info();
        keyArray = new String[]{infor.BQC_33, infor.BQC_31, infor.BQC_30, infor.BQC_13,
                infor.BQC_11, infor.BQC_10, infor.BQC_03, infor.BQC_01,
                infor.BQC_00};
    }

    @Override
    public void sureClick() {
        super.sureClick();
        int clickNum = 0;
        for (ZCBF_infor info : list_bqc) {
            int position = list_bqc.indexOf(info);
            if (info.isSelect()) {
                clickNum++;
            }
            infor.btnHGPojo.get(keyArray[position]).isSel = info.isSelect();
        }
        infor.setOnclikNum(clickNum);
        infor.setResult(getResult(infor));
    }

    //    @Override
//    protected void back() {
//        // TODO Auto-generated method stub
//        super.back();
//
//
//    }

    @Override
    protected JCInfo getResult() {
        return infor;
    }

    private void info() {

        view = LayoutInflater.from(context).inflate(R.layout.zc_dialog_bf, null);

        //中间
        ll_xxk = (LinearLayout) view.findViewById(R.id.zcbf_dialog_xxk);
        gr = (GridView) view.findViewById(R.id.zcbf_dlg_gr);
        adapter = new ZCBQC_DLG_GR_adapter(list_bqc);

        ll_xxk.setVisibility(View.GONE);

        center_ll.addView(view);
        //事件

        gr.setAdapter(adapter);

    }


    /**
     * 添加比分集合
     */
    private void addBiFen() {
        list_bqc.clear();

        for (int i = 0; i < infor.getTouZhuInfor().size(); i++) {
            if (i >= 45 && i <= 53) {//胜
                list_bqc.add(infor.getTouZhuInfor().get(i));
            }
        }

    }


    /**
     * 适配器
     *
     * @author Administrator
     */
    class ZCBQC_DLG_GR_adapter extends BaseAdapter {

        public List<ZCBF_infor> list = new ArrayList<ZCBF_infor>();
//		private int count = 0;

        public ZCBQC_DLG_GR_adapter(List<ZCBF_infor> list) {
            this.list = list;
        }

        @Override
        public int getCount() {
            // TODO Auto-generated method stub
            return list.size();
        }

        @Override
        public Object getItem(int position) {
            // TODO Auto-generated method stub
            return list.get(position);
        }

        @Override
        public long getItemId(int position) {
            // TODO Auto-generated method stub
            return position;
        }

        @Override
        public View getView(final int position, View view, ViewGroup parent) {
            if (position == 0) {
//				count = 0;
            }
            view = LayoutInflater.from(context).inflate(R.layout.zc_dialog_bf_gr,
                    null);
            final ViewHolder holder = new ViewHolder();

            holder.ll1 = (LinearLayout) view.findViewById(R.id.zcbf_dlg_gr_ll1);
            holder.tv1 = (TextView) view.findViewById(R.id.zcbf_dlg_gr_tv1);
            holder.tv2 = (TextView) view.findViewById(R.id.zcbf_dlg_gr_tv2);

            view.setTag(holder);

            holder.tv1.setText(list.get(position).getBiFen());
            holder.tv2.setText(list.get(position).getPeilv());

            if (!list.get(position).isSelect()) {
                holder.tv1.setTextColor(Color.BLACK);
                holder.tv2.setTextColor(Color.BLACK);
                holder.ll1.setBackgroundResource(R.drawable.zcbf_gr_click);
            } else {
                holder.tv1.setTextColor(Color.RED);
                holder.tv2.setTextColor(Color.RED);
                holder.ll1.setBackgroundResource(R.drawable.zcbf_gr_click2);
//				if(position != list.size()-1){
//					
//					count++;
//				}
//				Log.e("count", position+"  >>>>>>  "+count);
            }

            holder.ll1.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View v) {
//					if (position == list.size()-1) {
//						if (list.get(position).isSelect()) {
//							for (ZCBF_infor info : list) {
//								info.setSelect(false);
//							}
//						}else{
//							for (ZCBF_infor info : list) {
//								info.setSelect(true);
//							}
//							
//						}
//						ZCBQC_DLG_GR_adapter.this.notifyDataSetChanged();
//						return;
//					}

                    list.get(position).setSelect(!list.get(position).isSelect());
                    if (!list.get(position).isSelect()) {
                        holder.tv1.setTextColor(Color.BLACK);
                        holder.tv2.setTextColor(Color.BLACK);
                        holder.ll1.setBackgroundResource(R.drawable.zcbf_gr_click);
//						count--;
                    } else {
                        holder.tv1.setTextColor(Color.RED);
                        holder.tv2.setTextColor(Color.RED);
                        holder.ll1.setBackgroundResource(R.drawable.zcbf_gr_click2);
//						count++;
                    }
//					Log.e("count", ">>>>>>  "+count);
//					if (count == list.size()-1) {
//						list.get(list.size()-1).setSelect(true);
//						ZCBQC_DLG_GR_adapter.this.notifyDataSetChanged();
//					}else{
//						list.get(list.size()-1).setSelect(false);
//						ZCBQC_DLG_GR_adapter.this.notifyDataSetChanged();
//					}
                }
            });


            return view;
        }

        public List<ZCBF_infor> getList() {
            return list;
        }

        public void setList(List<ZCBF_infor> list) {
            this.list = list;
            this.notifyDataSetChanged();
        }

        @Override
        public void notifyDataSetChanged() {
//			count = 0;
            super.notifyDataSetChanged();
        }

        class ViewHolder {
            LinearLayout ll1;
            TextView tv1;
            TextView tv2;
        }

    }


}
